package com.cn.ccp.service.classschedule;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.RowMapper;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.TermSettingBean;
import com.cn.ccp.service.BasicService;
import com.cn.ccp.util.MsgConstants;

/**
 * service
 * @author Administrator
 *
 */
public class TermSettingService extends BasicService{
	private Log log = LogFactory.getLog(TermSettingService.class);
		
	/**
	 * 创建学期设置
	 * @param 
	 * @return
	 */
	public boolean save(TermSettingBean bo){
		log.info(bo);
		String sql = "insert into term_setting(term_year,tearm_type,first_week_start) values (?,?,?)";
		return jdbcTemplate.update(sql ,new Object[]{bo.getTermYear(),bo.getTermType(),bo.getFirstWeekStart()}) > 0;
	}
	

	public boolean checkIsAllready(TermSettingBean bo){
		String sql = "select count(1) from term_setting where term_year = ? and tearm_type = ? ";
		return jdbcTemplate.queryForInt(sql ,new Object[]{bo.getTermYear() ,bo.getTermType() }) > 0;
	}
	
	
	/**
	 * 更新学期设置
	 * @param bo
	 * @return
	 */
	public boolean update(TermSettingBean bo){
		log.info(bo);
		String sql = "update term_setting set term_year = ?, tearm_type = ? ,first_week_start = ? where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{bo.getTermYear(),bo.getTermType(),bo.getFirstWeekStart() ,bo.getId()}) > 0; 
	}
	
	
	
	/**
	 * 删除学期设置
	 * @param id
	 * @return
	 */
	public boolean delete(int id) {
		String sql = "delete from term_setting where id = ?";
		return jdbcTemplate.update(sql ,new Object[]{id}) > 0;
	}
	
	
	
	public Object findTermSettingById(int id) {
		String sql = "SELECT * FROM term_setting WHERE id = ?";
		return jdbcTemplate.queryForObject(sql, new Object[] { id }, new TermSettingMapper());
	}
	
	public List pageQueryTermSetting(int start, int pagesize) {
		String sql = "select * from term_setting limit ?,?";
		return jdbcTemplate.query(sql, new Object[] { start,pagesize},new TermSettingMapper());
	}
	
	/**
	 * 获取指定年份的当前和下一年的学期设置 
	 * @param curYear
	 * @return
	 */
	public List queryTerymByYear(int curYear){
		String sql = "select * from term_setting where term_year = ? or term_year = ? ";
		return jdbcTemplate.query(sql, new Object[] {curYear,curYear-1},new TermSettingMapper());
	}
	
	
	public int getCountTermSetting() {
		String sql = "select count(1) from term_setting ";
		return jdbcTemplate.queryForInt(sql);
	}
	
	
	public class TermSettingMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int i) throws SQLException {
			TermSettingBean bean = new TermSettingBean();
			bean.setId(rs.getInt("id"));
			bean.setTermYear(rs.getString("term_year"));
			bean.setTermType(rs.getString("tearm_type"));
			try{
				if(bean.getTermType().equals("1")){
					bean.setTermTypeName(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TERM_TYPE_LAST_TERM));
				}else if(bean.getTermType().equals("2")){
					bean.setTermTypeName(BaseAction.getInstance().getMessages(MsgConstants.CLASS_SCHEDULE_SETTING_APGE_TERM_TYPE_NEXT_TERM));
				}
			}catch(Exception e){}
			bean.setFirstWeekStart(rs.getString("first_week_start"));
			bean.setFirstWeekStart(bean.getFirstWeekStart().substring(0,10));
			return bean;
		}
	}
	
}
